package fate.springmvc.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import fate.car.pojo.ACar;
import fate.car.pojo.ACheck;
import fate.car.service.ICarsService;
import fate.common.controller.BaseController;
import fate.common.util.ContextUtil;
import fate.common.util.PageInfoUtil;

/**
 * 汽车控制层
 * 
 * @author Administrator
 *
 */
@Controller
public class ACarsController extends BaseController
{
	@Resource(name = "aICarsService")
	private ICarsService carsService;
	/**
	 * 打开汽车添加的页面
	 */
	@RequestMapping("/addcar")
	public String addCar()
	{
		ContextUtil.LOGGER.info("------addcar-----");

		return "addcar";
	}
	@RequestMapping("/addcarsSubmit")
	public String addCarsSubmit(ACar car, MultipartFile photo2)
	{
		String str1 = "E:/Program Files (x86)/MyJava/rent-car/WebContent/file";
		String str2 = dateUtil.dateToString(new Date());
		String photoFile = str1 + str2 + photo2.getOriginalFilename();
		System.out.println(photoFile);
		/* 直接调用方法存储上传的照片文件 */
		try
		{
			photo2.transferTo(new File(photoFile));
		} catch (IllegalStateException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		car.setImgPath(photoFile);
		int res = this.carsService.insertCar(car);
		System.out.println("结果：" + res);
		return "addcar";
	}

	/**
	 * 打开汽车查询的页面
	 */
	@RequestMapping("/querycar")
	public String queryCar(String number,String color,String price,String rentprice,
			String deposit,String status,String currentPage, String pageSize,Model model)
	{
		ContextUtil.LOGGER.info("==querycar===");
		/*
		 * 如果不传,默认可是null
		 * */
		Map<String, Object> condMap = new HashMap<String,Object>();
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		if(!"".equalsIgnoreCase(number))
		{
			condMap.put("number", number);
		}
		if(!"".equalsIgnoreCase(color))
		{
			condMap.put("color", color);
		}
		if(!"".equalsIgnoreCase(price))
		{
			condMap.put("price", price);
		}
		if(!"".equalsIgnoreCase(rentprice))
		{
			condMap.put("rentprice", rentprice);
		}
		if(!"".equalsIgnoreCase(deposit))
		{
			condMap.put("deposit", deposit);
		}
		PageInfoUtil pageInfoUtil = proccedPageInfoUtil(currentPage, pageSize);
		List<ACar> carList = this.carsService.findCondListCarsService(pageInfoUtil, condMap);
		/* 查询出的数据存储到四大作用域名中 */
		model.addAttribute("carList",carList);
		model.addAttribute("pageInfoUtil",pageInfoUtil);
		return "querycar";
	}
	//汽车出租
	@RequestMapping("/carlease")
	public String carLease()
	{
		
		return "carlease";
	}
	@RequestMapping("/showcar")
	public String showCar(String imgPath,String ctype,String color,String rentprice,String deposit,String currentPage, String pageSize,Model model)
	{
		/*
		 * 如果不传,默认可是null
		 * */
		Map<String, Object> condMap = new HashMap<String,Object>();
		if(!"".equalsIgnoreCase(imgPath))
		{
			condMap.put("imgPath", imgPath);
		}
		if(!"".equalsIgnoreCase(ctype))
		{
			condMap.put("ctype", ctype);
		}
		if(!"".equalsIgnoreCase(color))
		{
			condMap.put("color", color);
		}
		if(!"".equalsIgnoreCase(rentprice))
		{
			condMap.put("rentprice", rentprice);
		}
		if(!"".equalsIgnoreCase(deposit))
		{
			condMap.put("deposit", deposit);
		}
		PageInfoUtil pageInfoUtil = proccedPageInfoUtil(currentPage, pageSize);
		List<ACar> carList = this.carsService.ListCarsService(pageInfoUtil, condMap);
		/* 查询出的数据存储到四大作用域名中 */
		model.addAttribute("carList",carList);
		model.addAttribute("pageInfoUtil",pageInfoUtil);
		return "showcar";
	}
	@RequestMapping("/rentcar")
	public String rentCar()
	{
		
		return "rentcar";
	}
	//检查单查询
	@RequestMapping("/checklist")
	public String checkList(String adminsId,String checkdate,String problem,String paying,
			String currentPage, String pageSize,Model model)
	{
		/*
		 * 如果不传,默认可是null
		 * */
		Map<String, Object> condMap = new HashMap<String,Object>();
		if(!"".equalsIgnoreCase(adminsId))
		{
			condMap.put("adminsId", adminsId);
		}
		if(!"".equalsIgnoreCase(checkdate))
		{
			condMap.put("checkdate", checkdate);
		}
		if(!"".equalsIgnoreCase(problem))
		{
			condMap.put("problem", problem);
		}
		if(!"".equalsIgnoreCase(paying))
		{
			condMap.put("paying", paying);
		}
		PageInfoUtil pageInfoUtil = proccedPageInfoUtil(currentPage, pageSize);
		List<ACheck> checkList = this.carsService.ListCheckService(pageInfoUtil, condMap);
		/* 查询出的数据存储到四大作用域名中 */
		model.addAttribute("checkList",checkList);
		model.addAttribute("pageInfoUtil",pageInfoUtil);
		return "checklist";
	}
	//删除汽车信息
	@RequestMapping("/deletecars")
	public String deleteCars(String number)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("number", number);
		String i = this.carsService.deleteCars(condMap);
		System.out.println("结果：" + i);
		return "querycar";
	}
}
